<?php

/**
 *   OPENSHOP 管理中心套装商品类型管理
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: group_pro_goods_type.php,v 1.0 2009/06/19 09:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require_once(dirname(__FILE__).'/includes/init.php');
$exc = new exchange($os->table('group_promotion_goods_type'),$db,'goods_type_id','goods_type_name');

/* act操作项的初始化 */
if (empty($_REQUEST['act'])){
    $_REQUEST['act'] = 'list';
}else{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}
if($_REQUEST['act']=='list'){ 
	//检查权限
	admin_priv("view_group_pro_goods_type");  //by ywm  添加权限列表
	
	$group_pro_id = isset($_GET['group_pro_id']) ? intval($_GET['group_pro_id']) : 0; 
	$smarty->assign('ur_here',      '套装商品类型管理');   
	$smarty->assign('action_link',  array('href' => 'group_pro_goods_type.php?act=add&group_pro_id='.$group_pro_id, 'text' => '添加商品套装类型'));
	$group_pro_g_type_list = get_group_pro_goods_type_list($group_pro_id);
	$smarty->assign('group_pro_g_type_list',   $group_pro_g_type_list['group_pro_goods_type']);
    $smarty->assign('filter',           $group_pro_g_type_list['filter']);
    $smarty->assign('record_count',     $group_pro_g_type_list['record_count']);
    $smarty->assign('page_count',       $group_pro_g_type_list['page_count']);		

	$smarty->assign('full_page',  1);
	
    assign_query_info();
	
	$smarty->display('group_pro_goods_type_list.tpl');
	
}elseif($_REQUEST['act']=='add' || $_REQUEST['act']=='edit'){

    /* 添加还是编辑的标识 */
   $is_add = $_REQUEST['act'] == 'add';
    
        /* 权限检查 */
    $priv_str = $is_add ? 'add_group_pro_goods_type' : 'edit_group_pro_goods_type';
    admin_priv($priv_str);
    $guide = lattice_setting('view_group_pro_goods_type');   //by ywm  数据绑顶 菜单  
    
    $smarty->assign('form_act', $is_add ? 'insert' : 'update');
    
    $group_pro_id = isset($_GET['group_pro_id']) ? intval($_GET['group_pro_id']) : 0;
    
  
    
    $group_pro = $db->getRow("SELECT group_promotion_id,group_name,price_type FROM ".$os->table('group_promotion')." WHERE group_promotion_id='$group_pro_id'");
    if(empty($group_pro)){
    	os_header("Location: ./\n");
    }
    /* 取得商品套装类型信息 */
    if ($is_add){
	    $sql = "DELETE FROM " .$os->table('group_promotion_goods') .
	            " WHERE group_promotion_id = '$group_pro_id' AND goods_type_id=0 AND admin_id = '$_SESSION[admin_id]'";	
	    $db->query($sql);    	
        $group_pro_goods_type = array(
            'goods_type_id' => 0,
            'group_promotion_id' => $group_pro_id,
            'goods_type_name' => '',
            'goods_count' => 1,
            'is_present'  => 0,
            'add_money'   => ''
        );
    }else{
        $sql = "SELECT * FROM " . $os->table('group_promotion_goods_type') . " WHERE goods_type_id = '$_REQUEST[goods_type_id]'";
        $group_pro_goods_type = $db->getRow($sql);
    	$sql = "SELECT gpg.goods_id,g.goods_name FROM " . $GLOBALS['os']->table('group_promotion_goods') . " AS gpg ".
           	   " LEFT JOIN ". $GLOBALS['os']->table('goods') . " AS g ".
               "   ON g.goods_id = gpg.goods_id WHERE gpg.goods_type_id = " . intval($_GET['goods_type_id']) .
               " ORDER BY gpg.promotion_goods_id";

    	$pro_goods_list = $GLOBALS['db']->getAll($sql);
    	$smarty->assign('pro_goods_type_list', $pro_goods_list);
    }
    $smarty->assign('group_pro_goods_type',array_merge($group_pro_goods_type,array_slice($group_pro,1)));
    $smarty->assign('group_pro_list', group_pro_list($group_pro_goods_type['group_promotion_id']));
    $smarty->assign('cat_list',     cat_list());
    $smarty->assign('brand_list',   get_brand_list());

    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加商品套装类型');
    }
    else
    {
        $smarty->assign('ur_here', '编辑商品套装类型记录');
    }
    $href = 'group_pro_goods_type.php?act=list&group_pro_id='.$group_pro_id;
    if (!$is_add)
    {
        $href .= '&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '商品套装类型管理'));  
      
	assign_query_info();
	
	$smarty->display('group_pro_goods_type_info.tpl');
	
}elseif($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update'){
	/* 检查权限 */
    //admin_priv('group_pro_goods_type_manage');
       /* 添加还是编辑的标识 */
    //$is_add = $_REQUEST['act'] == 'add'; 
        /* 权限检查 */
    //$priv_str = $is_add ? 'add_group_pro_goods_type' : 'edit_group_pro_goods_type';
   // admin_priv($priv_str);
    $guide = lattice_setting('view_group_pro_goods_type');   //by ywm  数据绑顶 菜单  
    $priv_str = $is_add ? 'add_group_pro_goods_type' : 'edit_group_pro_goods_type';
    admin_priv($priv_str);
    
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert';	
    $group_pro_id = isset($_POST['group_promotion_id']) ? intval($_POST['group_promotion_id']) : 0;
    $group_pro = $db->getRow("SELECT group_promotion_id,price_type FROM " . $os->table('group_promotion') . " WHERE group_promotion_id = '$group_pro_id'");
    if(empty($group_pro)){
		os_header("Location: ./\n");    	
    }
    /* 提交值 */
    $group_pro_g_type = array(
        'goods_type_id'        => intval($_POST['goods_type_id']),
        'group_promotion_id'   => $group_pro_id,
        'goods_type_name'      => $_POST['goods_type_name'],
        'goods_count'          => intval($_POST['goods_count']),
        'is_present'           => intval($_POST['is_present']),
        'add_money'           =>  ($group_pro['price_type'] == 1 ? $_POST['add_money'] : ($_POST['is_present']==1 ? $_POST['add_money'] : 0.00))
    );
    /* 保存数据 */
    if ($is_add)
    {
        $db->autoExecute($os->table('group_promotion_goods_type'), $group_pro_g_type, 'INSERT');
        $group_pro_g_type['goods_type_id'] = $db->insert_id();
        handle_group_pro_goods($group_pro_g_type['goods_type_id']);
    }
    else
    {
        $db->autoExecute($os->table('group_promotion_goods_type'), $group_pro_g_type, 'UPDATE', "goods_type_id = '$group_pro_g_type[goods_type_id]'");
    }  
     /* 记日志 */
    if ($is_add){
        admin_log($group_pro_g_type['goods_type_name'], 'add', 'group_promotion_goods_type');
    }else{
        admin_log($group_pro_g_type['goods_type_name'], 'edit', 'group_promotion_goods_type');
    }    

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add){
        $links = array(
            array('href' => 'group_pro_goods_type.php?act=add&group_pro_id='.$group_pro_g_type['group_promotion_id'], 'text' => '继续添加商品套装类型'),
            array('href' => 'group_pro_goods_type.php?act=list&group_pro_id='.$group_pro_g_type['group_promotion_id'], 'text' => '返回商品套装类型列表')
        );
        sys_msg('添加商品套装类型成功', 0, $links);
    }else{
        $links = array(
            array('href' => 'group_pro_goods_type.php?act=list&group_pro_id='.$group_pro_g_type['group_promotion_id'].'&'.list_link_postfix(), 'text' => '返回商品套装类型列表')
        );
        sys_msg('编辑商品套装类型成功', 0, $links);
    } 
}elseif($_REQUEST['act'] == 'get_goods_list'){
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;

    $filters = $json->decode($_GET['JSON']);

    $arr = get_goods_list($filters);
    $opt = array();

    foreach ($arr AS $key => $val)
    {
        $opt[] = array('value' => $val['goods_id'],
                        'text' => $val['goods_name'],
                        'data' => $val['shop_price']);
    }

    make_json_result($opt);
}elseif($_REQUEST['act'] == 'add_group_pro_goods'){
    check_authz_json('group_pro_goods_manage');
    
    include_once(ROOT_PATH . 'includes/cls_json.php');
    
    $json = new JSON;
    $fittings   = $json->decode($_GET['add_ids']);
    $arguments  = $json->decode($_GET['JSON']);
    $group_pro_id = $arguments[0];
    $goods_type_id = $arguments[1];
    if($group_pro_id<=0)  make_json_error('您必须选择所属套装商品');
    foreach ($fittings AS $val)
    {
        $sql = "INSERT INTO " . $os->table('group_promotion_goods') . " (group_promotion_id, goods_type_id, goods_id, admin_id) " .
                "VALUES ('$group_pro_id', '$goods_type_id', '$val', '$_SESSION[admin_id]')";
        $db->query($sql, 'SILENT');
    }

    $arr = get_group_pro_goods($group_pro_id,$goods_type_id);
    $opt = array();
    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['goods_id'],
                       'text'       => $val['goods_name'],
                       'data'       => '');
    }

    clear_cache_files();
    make_json_result($opt);	
}elseif($_REQUEST['act']=='drop_group_pro_goods'){
    check_authz_json('delete_group_pro_goods_type');//  delete_group_pro_goods_type
    
    include_once(ROOT_PATH . 'includes/cls_json.php');
    
    $json = new JSON;
    $fittings   = $json->decode($_GET['drop_ids']);
    $arguments  = $json->decode($_GET['JSON']);
    $group_pro_id = $arguments[0];
    $goods_type_id = $arguments[1];

    $sql = "DELETE FROM " .$os->table('group_promotion_goods') .
            " WHERE group_promotion_id='$group_pro_id' AND goods_type_id='$goods_type_id' AND " 
            .db_create_in($fittings, 'goods_id');
    if ($goods_type_id == 0)
    {
        $sql .= " AND admin_id = '$_SESSION[admin_id]'";
    }
    $db->query($sql);

    $arr = get_group_pro_goods($group_pro_id,$goods_type_id);
    $opt = array();

    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['goods_id'],
                       'text'       => $val['goods_name'],
                       'data'       => '');
    }

    clear_cache_files();
    make_json_result($opt);	
}elseif ($_REQUEST['act']=='remove'){ 
	/* 检查权限 */
    check_authz_json('delete_group_pro_goods_type');  //by ywm 
    $gpg_type_id = intval($_GET['id']);
    $group_pro_goods_type = $db->getRow("SELECT group_promotion_id,goods_type_name FROM ".$os->table('group_promotion_goods_type')." WHERE goods_type_id=".$gpg_type_id);
    if (empty($group_pro_goods_type))
    {
        make_json_error('您要操作的商品套装类型不存在');
    }    
    $group_pro_goods_count = $db->getOne('SELECT COUNT(*) FROM ' .$os->table('group_promotion_goods'). " WHERE goods_type_id='$gpg_type_id'");
    if($group_pro_goods_count==0){
	    /* 删除该颜色 */
	    if($exc->drop($gpg_type_id)){
		    /* 记日志 */
		    admin_log($group_pro_goods_type['goods_type_name'], 'remove', 'group_promotion_goods_type');   	
	    }
    }else{
    	make_json_error('"'. $group_pro_goods_type['goods_type_name'] .'" 该商品套装类型与商品还有关联,您不能删除!');
    }
    /* 清除缓存 */
    clear_cache_files();

    $url = 'group_pro_goods_type.php?act=query&group_pro_id='.$group_pro_goods_type['group_promotion_id'].'&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    os_header("Location: $url\n");
    exit;	
}elseif($_REQUEST['act']=='query'){
    $list = get_group_pro_goods_type_list($_GET['group_pro_id']);

    $smarty->assign('group_pro_g_type_list',  $list['group_pro_goods_type']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);
    
    make_json_result($smarty->fetch('group_pro_goods_type_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));	
}

function handle_group_pro_goods($goods_type_id)
{
    $sql = "UPDATE " . $GLOBALS['os']->table('group_promotion_goods') . " SET " .
            " goods_type_id = '$goods_type_id' " .
            " WHERE goods_type_id = '0'" .
            " AND admin_id = '$_SESSION[admin_id]'";
    $GLOBALS['db']->query($sql);
}

function get_group_pro_goods($group_pro_id,$goods_type_id){
    $sql = "SELECT gpd.goods_id, g.goods_name FROM " . $GLOBALS['os']->table('group_promotion_goods') .
           " AS gpd, " . $GLOBALS['os']->table('goods') . " AS g " . 
           "WHERE gpd.group_promotion_id = '$group_pro_id' AND gpd.goods_type_id = '$goods_type_id' " .
            "AND gpd.goods_id = g.goods_id ";
    if ($goods_type_id == 0)
    {
        $sql .= " AND gpd.admin_id = '$_SESSION[admin_id]'";
    }
    $row = $GLOBALS['db']->getAll($sql);

    return $row;
}

function group_pro_list($selected)
{
    $sql = 'SELECT group_promotion_id, group_name FROM ' . $GLOBALS['os']->table('group_promotion');
    $res = $GLOBALS['db']->query($sql);

    $lst = '';
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $lst .= "<option value='$row[group_promotion_id]'";
        $lst .= ($selected == $row['group_promotion_id']) ? ' selected="true"' : '';
        $lst .= '>' . htmlspecialchars($row['group_name']). '</option>';
    }

    return $lst;
}

function get_group_pro_goods_type_list($group_pro_id){
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['os']->table('group_promotion_goods_type');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 */
		$sql = "SELECT g_y.*,g_p.group_name,g_p.price_type FROM ". $GLOBALS['os']->table('group_promotion_goods_type') .
		       " g_y LEFT JOIN ". $GLOBALS['os']->table('group_promotion') ." g_p ON 
		       g_y.group_promotion_id=g_p.group_promotion_id WHERE g_y.group_promotion_id='$group_pro_id' ORDER BY g_y.goods_type_id DESC";

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);
    
    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $rows;
    }	
    
    return array('group_pro_goods_type' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}
?>